package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author : jiang
 * @date : 2023/10/16 20:11
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //执行查询操作
        Page<SetmealVO> setmeals = setmealMapper.page(setmealPageQueryDTO);
        //封装数据返回
        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //拷贝数据
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //执行修改操作
        setmealMapper.update(setmeal);
        //获取修改套餐的id
        Long setmealId = setmeal.getId();
        //删除套餐菜品表中该套餐关联的数据
        setmealDishMapper.deleteById(Arrays.asList(setmealId));
        //获取套餐关联的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断菜品是否为空
        if (setmealDishes != null && setmealDishes.size() > 0){
            //操作套餐菜品表
            setmealDishes.forEach(dish -> {
                dish.setSetmealId(setmealId);
            });
            setmealDishMapper.addSetmealDish(setmealDishes);
        }
    }

    /**
     * 修改套餐状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //创建套餐对象
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();

        //获取套餐中关联的所有菜品 如果有菜品处于停售状态，则不能将套餐的状态改成起售
        List<SetmealDish> dishList = setmealDishMapper.getDishBySetmealId(id);
        dishList.forEach(dish->{
            Dish d = dishMapper.gatById(dish.getDishId());
            if (d.getStatus().equals(StatusConstant.DISABLE)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });
        //修改数据库数据
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐信息
        Setmeal setmeal = setmealMapper.getById(id);
        //获取套餐对应的分类名称
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryMapper.getCategoryById(categoryId);
        String name = category.getName();
        //获取套餐关联的菜品
        List<SetmealDish> dish = setmealDishMapper.getDishBySetmealId(id);

        //创建套餐信息对象
        SetmealVO setmealVO = new SetmealVO();
        //拷贝数据
        BeanUtils.copyProperties(setmeal,setmealVO);
        //补充数据
        setmealVO.setCategoryName(name);
        setmealVO.setSetmealDishes(dish);

        return setmealVO;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSetmeal(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //拷贝数据
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //插入数据
        setmealMapper.addSetmeal(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //获取套餐关联的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断菜品是否为空
        if (setmealDishes != null && setmealDishes.size() >0){
            //设置套餐id
            setmealDishes.forEach(dish -> {
                dish.setSetmealId(setmealId);
            });
            //添加菜品到套餐菜品表
            setmealDishMapper.addSetmealDish(setmealDishes);
        }

    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        //判断套餐是否起售
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);
            Integer status = setmeal.getStatus();
            if (status.equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐表中的套餐
        setmealMapper.deleteById(ids);
        //删除套餐关联的菜品
        setmealDishMapper.deleteById(ids);
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        List<Setmeal> list = setmealMapper.getSetmealBycategoryId(categoryId,StatusConstant.ENABLE);
        return list;
    }

    /**
     * 根据套餐id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long id) {
        List<DishItemVO> dishes = setmealDishMapper.getDishBySetmeal(id);
        return dishes;
    }

}
